import { Sprite } from 'pixi.js';
import BaseSprite from './BaseSprite.js';
import ResourceManager from '../resource.js';
import AudioManager from '../audio.js';
import EventManager from '../event.js';

class MarioSprite extends BaseSprite {
    constructor(app) {
        super(app, 'mario');
        
        this.sprite = new Sprite();
        this.isJumping = false;
        this.isMoveLeft = false;
        this.isMoveRight = false;
        this.facing = 'right';
        this.scale = 2;
        this.velocity = -15;
        this.initialVelocity = -15;
        this.gravity = 0.64;
        this.speed = 5;
        this.groundY = 0;
        
        this.resources = {
            mario_left: './mario/left.png',
            mario_right: './mario/right.png',
            mario_jump_left: './mario/jump_left.png',
            mario_jump_right: './mario/jump_right.png'
        };
    }

    async loadResources() {
        for (const [name, url] of Object.entries(this.resources)) {
            await ResourceManager.load(name, url);
        }
    }

    async init() {
        this.groundY = 640 - 64 * 2 - 72;
        
        const rightTexture = ResourceManager.get('mario_right');
        this.sprite = new Sprite(rightTexture);
        this.sprite.anchor.x = 0.5;
        this.sprite.width = 32 * this.scale;
        this.sprite.height = 64 * this.scale;
        this.sprite.x = 320; // 屏幕中央
        this.sprite.y = this.groundY;
        
        this.addSprite(this.sprite);
    }

    registerEvents() {
        EventManager.on('mario_jump', this.handleJump.bind(this));
        EventManager.on('mario_move_left', this.handleMoveLeft.bind(this));
        EventManager.on('mario_move_right', this.handleMoveRight.bind(this));
        EventManager.on('mario_stop_left', this.handleStopLeft.bind(this));
        EventManager.on('mario_stop_right', this.handleStopRight.bind(this));
    }

    unregisterEvents() {
        EventManager.off('mario_jump');
        EventManager.off('mario_move_left');
        EventManager.off('mario_move_right');
        EventManager.off('mario_stop_left');
        EventManager.off('mario_stop_right');
    }

    handleJump() {
        if (this.jump()) {
            AudioManager.playJumpSound();
        }
    }

    handleMoveLeft() {
        this.moveLeft();
    }

    handleMoveRight() {
        this.moveRight();
    }

    handleStopLeft() {
        this.stopMoveLeft();
    }

    handleStopRight() {
        this.stopMoveRight();
    }

    jump() {
        if (!this.isJumping) {
            this.isJumping = true;
            this.velocity = this.initialVelocity;
            return true;
        }
        return false;
    }

    moveLeft() {
        this.facing = 'left';
        this.isMoveLeft = true;
    }

    moveRight() {
        this.facing = 'right';
        this.isMoveRight = true;
    }

    stopMoveLeft() {
        this.isMoveLeft = false;
    }

    stopMoveRight() {
        this.isMoveRight = false;
    }

    updateTexture() {
        if (this.isJumping) {
            if (this.facing === 'right') {
                this.sprite.texture = ResourceManager.get('mario_jump_right');
            } else {
                this.sprite.texture = ResourceManager.get('mario_jump_left');
            }
        } else {
            if (this.facing === 'right') {
                this.sprite.texture = ResourceManager.get('mario_right');
            } else {
                this.sprite.texture = ResourceManager.get('mario_left');
            }
        }
    }

    updateJumping() {
        if (this.isJumping) {
            this.sprite.y += this.velocity;
            this.velocity += this.gravity;

            if (this.sprite.y >= this.groundY) {
                this.sprite.y = this.groundY;
                this.isJumping = false;
                this.velocity = this.initialVelocity;
            }
            this.updateTexture();
        }
    }

    updateMoving() {
        if (this.isMoveLeft) {
            this.sprite.x -= this.speed;
            if (!this.isJumping) {
                this.updateTexture();
            }
        }

        if (this.isMoveRight) {
            this.sprite.x += this.speed;
            if (!this.isJumping) {
                this.updateTexture();
            }
        }
    }

    update(deltaTime) {
        if (!this.active) return;
        
        this.updateJumping();
        this.updateMoving();
    }

    setPosition(x, y) {
        if (this.sprite) {
            this.sprite.x = x;
            this.sprite.y = y;
        }
    }

    getPosition() {
        return {
            x: this.sprite.x,
            y: this.sprite.y
        };
    }
}

export default MarioSprite;
